package com.lecture.excel;

import com.alibaba.excel.EasyExcel;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EasyExcelMergeExample {
    public static void main(String[] args) {
        // 1. 准备测试数据
        List<DemoData> dataList = new ArrayList<>();
        dataList.add(new DemoData("研发部", "张三", "工程师", "1001"));
        dataList.add(new DemoData("研发部", "李四", "工程师", "1002"));
        dataList.add(new DemoData("研发部", "王五", "经理", "1003"));
        dataList.add(new DemoData("市场部", "赵六", "主管", "1004"));
        dataList.add(new DemoData("市场部", "钱七", "专员", "1005"));
        dataList.add(new DemoData("人事部", "孙八", "经理", "1006"));
        dataList.add(new DemoData("人事部", "周九", "专员", "1007"));
        dataList.add(new DemoData("人事部", "吴十", "专员", "1008"));
        //序号列和运营公司列需要合并单元格
        List<List<String>> mergeColDataList = Stream.of(dataList.stream().map(DemoData::getDepartment).collect(Collectors.toList()),
                dataList.stream().map(DemoData::getName).collect(Collectors.toList())).collect(Collectors.toList());
        //第一列(index=0)和第二列(index=1)
        List<Integer> mergeColIndexList = Stream.of(0, 1).collect(Collectors.toList());
        //内部finish的时候会自动关闭流
        EasyExcel.write("D:\\test.xlsx", DemoData.class)
                .registerWriteHandler(
                        new ExcelFillCellMergeStrategy(
                                mergeColDataList, mergeColIndexList
                        )
                )
                .sheet("统计")
                .doWrite(dataList);

    }
}
